<?php
namespace app\admin\controller;

use app\common\component\user_vip\UserVipModel;
use app\common\component\user\UserModel;
use app\common\component\user_wallet\UserWalletService;
use app\common\component\user_wallet_log\UserWalletLogModel;
use app\common\component\user_wallet_log\UserWalletLogService;
use app\common\component\user_wallet_vip\UserWalletVipService;
use app\common\component\user_wallet_vip_log\UserWalletVipLogModel;

class UserTopUp extends ControllerBase {
	public function check() {
		$this -> view -> assign('user_wallet_logs', (new UserWalletLogService()) -> getAll());

		return $this -> view -> fetch();
	}

	public function onCheck() {
		$getUserWalletLog = UserWalletLogModel::get(['user_wallet_log_id' => $this -> request -> get('user_wallet_log_id', 0, 'int')]);
		$getUserWalletLog -> is_state = 1;
		\think\Db::startTrans();
		try {
			if($getUserWalletLog -> save()) {
				$userModel = UserModel::get(['username' => $getUserWalletLog -> to_user_name]);

				$resultUserWallet = (new UserWalletService()) -> setIncMoney($getUserWalletLog -> price, $userModel -> user_id);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();
			echo '<meta charset="utf-8"><script>alert(' . $e -> getMessage() . ');window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("审核成功");window.location.href="/shuashuale/user/top_up/check";</script>';exit;
	}

	public function vip() {
		$userWalletVipLogs = UserWalletVipLogModel::alias('u_w_v_l')
			-> join('league_user u', 'u_w_v_l.user_id = u.user_id')
			-> field(
				'u_w_v_l.user_wallet_vip_log_id, u_w_v_l.user_id, u_w_v_l.type, u_w_v_l.active, u_w_v_l.price, u_w_v_l.new_price, u_w_v_l.old_price, u_w_v_l.create_time, u_w_v_l.voucher, u_w_v_l.is_state, ' .
				'u.username, u.realname, u.phone, u.bank_code'
			)
			-> order('u_w_v_l.is_state ASC, u_w_v_l.user_wallet_vip_log_id DESC')
			-> paginate(10);

		$this -> view -> assign('UserWalletVipLogs', $userWalletVipLogs);

		return $this -> view -> fetch();
	}

	public function checkVip() {
		$getUserWalletVipLog = UserWalletVipLogModel::get(['user_wallet_vip_log_id' => $this -> request -> get('user_wallet_vip_log_id', 0, 'int')]);
		$getUserWalletVipLog -> is_state = 1;

		$resultUserWalletVip = (new UserWalletVipService()) -> getUserWalletVipV01($getUserWalletVipLog -> user_id);
		$getUserWalletVipLog -> old_price = $resultUserWalletVip['data']['user_wallet_vip'] -> price;

		\think\Db::startTrans();
		try {
			if(!$resultUserWalletVip['data']['user_wallet_vip'] -> setInc('price', $getUserWalletVipLog -> price)) {
				throw new \Exception('审核失败');
			}

			$getUserWalletVipLog -> new_price = $resultUserWalletVip['data']['user_wallet_vip'] -> price;
			if(!$getUserWalletVipLog -> save()) {
				throw new \Exception('审核失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			echo '<meta charset="utf-8"><script>alert(' . $e -> getMessage() . ');window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("审核成功");window.location.href="/shuashuale/user/top_up/vip";</script>';exit;
	}

	public function money() {
		return $this -> view -> fetch();
	}
}
?>